package com.zhang.limit;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * semaphore类型限流demo
 */
public class SemaphoreLimitDemo {
    public static void main(String[] args) {
        // 线程池
        ExecutorService executorService = Executors.newCachedThreadPool();
        // 信号量，只允许 5个线程同时访问
        Semaphore semaphore = new Semaphore(5);
        // 模拟客户端请求
        for (int i = 0; i < 20; i++) {
            final long num = i;
            executorService.submit(() -> {
                try {
                    // 获取许可
                    semaphore.acquire();
                    System.out.println("当前成功====获取者====:" + num);
                    // 模拟执行时间
                    Thread.sleep(new Random().nextInt(10000));
                    // 释放
                    semaphore.release();
                    // 打印释放
                    System.out.println("当前成功++++释放者++++:" + num);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        try {
            Thread.sleep(10);
            System.out.println("执行完毕！");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //退出线程池
        executorService.shutdown();
    }
}
